#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

// Validate time components (seconds and minutes must be 0-59)
int validate_time_parts(int count, const int *values)
{
    switch (count)
    {
    case 1: // SS
        return (values[0] >= 0 && values[0] <= 59) ? 0 : -1;
    case 2: // MM:SS
        return (values[0] >= 0 && values[1] >= 0 &&
                values[1] <= 59)
                   ? 0
                   : -1; // Minutes can be any (e.g., 120:30 is valid)
    case 3:              // HH:MM:SS
        return (values[0] >= 0 && values[1] >= 0 && values[2] >= 0 &&
                values[1] <= 59 && values[2] <= 59)
                   ? 0
                   : -1;
    default:
        return -1;
    }
}

// Parse time string (HH:MM:SS, MM:SS, or SS) to total seconds
int parse_time_to_seconds(const char *time_str, int *result)
{
    if (!time_str || !result)
    {
        fprintf(stderr, "Invalid input (NULL pointers)\n");
        return -1;
    }

    // Handle empty string
    if (strlen(time_str) == 0)
    {
        fprintf(stderr, "Empty time string\n");
        return -1;
    }

    char *copy = strdup(time_str); // Create modifiable copy
    if (!copy)
    {
        fprintf(stderr, "Memory allocation failed\n");
        return -1;
    }

    char *parts[3] = {NULL}; // Store up to 3 parts (HH, MM, SS)
    int count = 0;

    // Split string by colons
    char *token = strtok(copy, ":");
    while (token && count < 3)
    {
        parts[count++] = token;
        token = strtok(NULL, ":");
    }

    // Validate and convert parts to integers
    int values[3] = {0};
    for (int i = 0; i < count; i++)
    {
        char *endptr;
        long val = strtol(parts[i], &endptr, 10);

        // Check for non-numeric characters or overflow
        if (*endptr != '\0' || val < 0 || val > INT_MAX)
        {
            free(copy);
            fprintf(stderr, "Invalid time part: '%s' (non-numeric or out of range)\n", parts[i]);
            return -1;
        }
        values[i] = (int)val;
    }

    // Validate time ranges (e.g., seconds <=59)
    if (validate_time_parts(count, values) != 0)
    {
        free(copy);
        fprintf(stderr, "Invalid time range in: '%s' (seconds/minutes must be 0-59)\n", time_str);
        return -1;
    }

    // Calculate total seconds
    int total = 0;
    switch (count)
    {
    case 1:
        total = values[0];
        break; // SS
    case 2:
        total = values[0] * 60 + values[1];
        break; // MM:SS
    case 3:
        total = values[0] * 3600 + values[1] * 60 + values[2];
        break; // HH:MM:SS
    default:
        free(copy);
        fprintf(stderr, "Too many time parts (max 3: HH:MM:SS)\n");
        return -1;
    }

    *result = total;
    free(copy);
    return 0;
}

// Example usage with clean test cases
int main()
{
    const char *test_cases[] = {
        "45",      // Valid: 45s
        "2:30",    // Valid: 2m30s → 150s
        "1:20:15", // Valid: 1h20m15s → 4815s
        "0:05:00", // Valid: 0h5m0s → 300s
        "120:30",  // Valid: 120m30s → 7230s (minutes can exceed 59)
        "invalid", // Invalid: non-numeric
        "1:61",    // Invalid: 61 seconds (must be ≤59)
        "2:30:60", // Invalid: 60 seconds (must be ≤59)
        "3:70:20", // Invalid: 70 minutes (must be ≤59)
        "",        // Invalid: empty string
        NULL       // End of test cases
    };

    for (int i = 0; test_cases[i] != NULL; i++)
    {
        int seconds;
        int ret = parse_time_to_seconds(test_cases[i], &seconds);
        if (ret == 0)
        {
            printf("Input: \"%s\" → %d seconds\n", test_cases[i], seconds);
        }
        else
        {
            printf("Input: \"%s\" → Parsing failed\n", test_cases[i]);
        }
    }

    return 0;
}
